package com.reggie.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.reggie.Exception.DishStatusException;
import com.reggie.common.R;
import com.reggie.dto.SetmealDto;
import com.reggie.pojo.Category;
import com.reggie.pojo.Dish;
import com.reggie.pojo.Setmeal;
import com.reggie.pojo.SetmealDish;
import com.reggie.service.impl.CategoryServiceImpl;
import com.reggie.service.impl.DishServiceImpl;
import com.reggie.service.impl.SetMealDishServiceImpl;
import com.reggie.service.impl.SetMealServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/setmeal")
@Slf4j
@Transactional
public class SetMealController {

    @Autowired
    private SetMealServiceImpl setMealService;

    @Autowired
    private CategoryServiceImpl categoryService;
    @Autowired
    private SetMealDishServiceImpl setMealDishService;
    @Autowired
    private DishServiceImpl dishService;


    @GetMapping("/page")
    public R<Page<SetmealDto>> selectByPage(Integer page, Integer pageSize, String name) {
        //套餐的分页查询功能大体与菜品的分页查询类似
        log.info("进入分页查询服务器");
        Page<Setmeal> setmealPage_now = setMealService.selectByPage(page, pageSize, name);
        Page<SetmealDto> page_SetMealDto = new Page<>();
        BeanUtils.copyProperties(setmealPage_now, page_SetMealDto, "records");
        List<SetmealDto> setmealDtos = setmealPage_now.getRecords().stream().map((item) -> {
            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(item, setmealDto);
            String categoryName = categoryService.selectCategoryNameById(item.getCategoryId());
            setmealDto.setCategoryName(categoryName);
            return setmealDto;
        }).collect(Collectors.toList());
        page_SetMealDto.setRecords(setmealDtos);
        return R.success(page_SetMealDto);
    }


    @PostMapping
    @Transactional
    public R<String> addSetMeal(@RequestBody SetmealDto setmealDto) {
        log.info("进入添加套餐服务器中");
        log.info("获取到的数据有{}", setmealDto);
        //还是同样的配方，将setmealDto中的数据进行分离
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDto, setmeal);
        log.info("套餐信息为:{}", setmeal);
        //先将套餐添加到表中
        setMealService.addSetMeal(setmeal);
        //分离dto中的菜单信息，并将菜单信息添加到setmealDish表中
        setmealDto.getSetmealDishes().forEach((item) -> {
            //如果这道菜状态为禁用就报错
            Dish dish = dishService.selectById(item.getDishId());
            if (dish.getStatus() == 0) {
                throw new DishStatusException("套餐中有菜品已经下架了！请重新挑选吧...");
            }
            item.setSetmealId(setmeal.getId());
            setMealDishService.addSetMealDish(item);
        });
        return R.success("添加成功！");
    }

    //修改套餐状态
    @Transactional
    @PostMapping("/status/{status}")
    public R<String> updateStatus(@PathVariable Integer status, Long... ids) {
        log.info("修改状态为{},要修改的套餐的id是{}", status, ids);
        boolean b = setMealService.updateStatusById(status, ids);
        if (b) {
            return R.success("修改状态成功！");
        } else {
            return R.error("修改失败！");
        }
    }

    //通过id进行删除
    @DeleteMapping
    @Transactional
    public R<String> deleteById(Long... ids) {
        System.out.println(Arrays.toString(ids));
        //执行删除业务逻辑
        //分别执行套餐表和套餐菜品表的删除逻辑
        setMealService.deleteById(ids);
        setMealDishService.deleteBySetMealId(ids);
        return R.success("删除成功！");
    }

    //回显套餐数据
    @Transactional
    @GetMapping("/{id}")
    public R<SetmealDto> selectById(@PathVariable Long id) {
        System.out.println("回显数据服务器");
        Setmeal setmeal = setMealService.selectById(id);
        SetmealDto setmealDto = new SetmealDto();
        BeanUtils.copyProperties(setmeal, setmealDto);
        List<SetmealDish> setmealDishes = setMealDishService.selectBySetmealId(id);
        setmealDto.setSetmealDishes(setmealDishes);
        System.out.println(setmealDto);
        return R.success(setmealDto);
    }

    //修改操作
    @PutMapping
    public R<String> updateSetMeal(@RequestBody SetmealDto setmealDto) {
        //分离数据进行后续操作
        Setmeal setmeal = new Setmeal();
        //将套餐数据考到套餐类中
        BeanUtils.copyProperties(setmealDto, setmeal);
        //将套餐中的菜品信息考到套餐菜品类中
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(setmealDto.getId());
        }

        //判断一下要修改的这个套餐中的菜品的状态是不是启用状态如果不是就报错
        for (SetmealDish setmealDish : setmealDishes) {
            Dish dish = dishService.selectById(setmealDish.getDishId());
            if (dish.getStatus() == 0) {
                throw new DishStatusException("套餐中有菜品已经下架了！请重新挑选吧...");
            }
        }
        //先修改套餐表中的数据
        setMealService.updateSetmeal(setmeal);
        //再修改套餐菜品表中的数据
        setMealDishService.updateSetMealDish(setmealDishes);
        return R.success("修改成功！");
    }

    @GetMapping("/list")
    public R<List<Setmeal>> getCategory(Long categoryId, Integer status) {
        log.info("查询套餐服务器");
        System.out.println(categoryId+":"+status);
        List<Setmeal> setmeals = setMealService.selectSetmealByCategoryId(categoryId, status);
        System.out.println(setmeals);
        return R.success(setmeals);
    }
}
